<template>
  <div class="auto-archive-container">
    <div class="order-archive-header">
      <!-- <span class="auto-archive-table-header"> 维修工单 </span> -->
      <el-form ref="RepairOrder" :model="RepairOrder" label-width="110px" class="auto-archive-body-header">
        <el-form-item label="申请人：" class="auto-archive-input">
          <el-input v-model="RepairOrder.userName"></el-input>
        </el-form-item>
        <el-form-item label="申请日期：" class="auto-archive-select">
          <el-date-picker v-model="RepairOrder.submitTime" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>

        <el-button type="primary" class="auto-archive-button">查询</el-button>

      </el-form>

    </div>

    <div class="order-archive-body">
      <div class="auto-archive-table">
        <el-table :data="this.filtedTableData" style="width: 100%">
          <el-table-column prop="repairOrderId" label="维修工单号" align="center">
          </el-table-column>
          <el-table-column prop="userName" label="申请人" align="center">
          </el-table-column>
          <el-table-column prop="submitTime" label="申请日期" align="center">
          </el-table-column>

          <el-table-column prop="repairOrderStatus" label="工单状态" align="center">
          </el-table-column>
          <el-table-column prop="action" label="操作" align="center">
            <template slot-scope="scope">
              <el-button @click="ToRepairOrderDetail(scope.row)" type="text" size="small">详情</el-button>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <div class="pagination">
        <el-pagination background layout="total, sizes, prev, pager, next, jumper" :current-page="tablePage.pageNum"
          :page-size="tablePage.pageSize" :page-sizes="pageSizes" :total="tablePage.total" @size-change="handleSizeChange"
          @current-change="handlePageChange" style="padding-right: 30px;" />
      </div>
    </div>


  </div>
</template>

<script>
import { repairPageQuery } from '@/api/order'
export default {
  name: "RepairOrderArchives",
  data() {
    return {
      tableData: [],
      RepairOrder: {
        submitTime: null,
        userName: null,
      },
      //分页相关数据
      tablePage: {
        pageNum: 1, // 第几页
        pageSize: 7, // 每页多少条
        total: 0 // 总记录数
      },
      BasicDataPage: null,
      pageSizes: [4, 7],
    }
  },
  computed: {
    filtedTableData() {
      if (!this.RepairOrder.userName && !this.RepairOrder.submitTime) {
        return this.getPageList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
      } else {
        return this.getPageList(this.tableData.filter((value) => {
          const nameMatch = !this.RepairOrder.userName || value.userName.includes(this.RepairOrder.userName);
          const timeMatch = !this.RepairOrder.submitTime || value.submitTime.includes(this.RepairOrder.submitTime);
          return nameMatch && timeMatch;
        }), this.tablePage.pageNum, this.tablePage.pageSize)
      }
    },

  },
  methods: {
    initTableData() {
      repairPageQuery().then(res => {
        this.tableData = res.data.data;
      })
    },
    // 分页
    handlePageChange(currentPage) {
      this.tablePage.pageNum = currentPage
      // 在此刷新数据
      this.BasicDataPage = this.getList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
    },
    handleSizeChange(pageSize) {
      this.tablePage.pageSize = pageSize
      // 在此刷新数据
      this.BasicDataPage = this.getList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
    },
    getList(data, num, size) {
      let list, start, end
      start = (num - 1) * size
      end = start + size
      list = data.filter((item, index) => {
        return index >= start && index < end
      })
      list.forEach((item, index) => {
        item.seq = index + start
      })
      return list
    },
    getPageList(data, num, size) {
      this.tablePage.total = data.length
      this.BasicDataPage = this.getList(data, num, size)
      return this.BasicDataPage
    },
    ToRepairOrderDetail(row) {
      this.$router.push({
        path: "/Home/RepairOrderDetail",
        query: {
          repairOrderId: row.repairOrderId
        }
      });
    }
  },
  mounted() {
    // this.initTableData();
    repairPageQuery().then(res => {
      this.tableData = res.data.data;
    })
  }
}
</script>

<style scoped>
.auto-archive-container {
  height: 100%;
  width: 95%;
  margin: 0 auto;
  background-color: #fff;
}

.auto-archive-table {
  width: 90%;
  margin: 0px auto;
}

.auto-archive-title {
  font-size: xx-large;
  justify-content: left;
  width: 10%;
}


.auto-archive-input {
  margin: 0 10px;
  width: 10%;
}

.order-archive-header {
  display: flex;
}

.order-archive-body {
  width: 100%;
  height: 40%;
  margin: 10px auto;
}

.order-archive-table-header {
  font-size: xx-large;
  justify-content: left;
  width: 10%;
}

.auto-archive-body-header {
  width: 90%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: right;
}

.auto-archive-footer {
  width: 70%;
  height: 10%;
  margin: 0 auto;
}

.auto-archive-select {
  margin: 0 30px;
  width: 20%;

}

.auto-archive-select-item {
  width: 99%;
}

.auto-archive-input {
  margin: 0;
  width: 35%;
}

.auto-archive-button {
  margin: 0 30px;
}

.pagination {
  width: 100%;
  margin: 25px auto;
  /* text-align: right; */
}
</style>